/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import java.awt.Color;
import java.io.Serializable;
import javax.swing.JLabel;
import org.math.plot.Plot2DPanel;
import org.math.plot.canvas.Plot2DCanvas;
import org.math.plot.plots.LinePlot;
import org.math.plot.plots.Plot;
import org.math.plot.utils.Array;

/**
 *
 * @author minaev
 */
public class Func implements Serializable {

    private String name;
    private String labelX = "";
    private String labelY = "";
    private double[] values;
    private double[] args = null;
    private double begin;
    private double samp;
    private int type;
    public final static Color[] COLORLIST = {Color.RED, Color.GREEN,
        Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA};
    public static final int F_OTHER = 0;
    public static final int F_PROCESS = 1;
    public static final int F_POSITIONS = 2;
    public static final int F_KFUNC = 3;
    public static final int F_SPECT = 4;
    public static final String[] names_type = {
        "Прочее",
        "Процесс",
        "Позиция",
        "Кор. Функция",
        "Спектр"
    };

    public Func(String name, int type, double[] values,
            double begin, double samp) {
        this.name = name;
        this.values = values;
        this.begin = begin;
        this.samp = samp;
        this.type = type;
    }

    public Func(String name, int type, double[] values, double[] args) {
        this.name = name;
        this.values = values;
        this.samp = 0;
        this.type = type;
        this.args = args;
    }

    public Func(String name, int type, double[][] val_args) {
        if (val_args.length == 2) {
            this.name = name;
            this.values = val_args[0];
            this.samp = 0;
            this.type = type;
            this.args = val_args[1];
        } else if (val_args.length == 3) {
            this.name = name;
            this.values = val_args[0];
            this.begin = val_args[1][0];
            this.samp = val_args[2][0];
            this.type = type;
        }
    }

    public String getName() {
        return name;
    }

    public double[] getValues() {
        return values;
    }

    public double[] getArgs() {
        return args;
    }

    public double getBegin() {
        return begin;
    }

    public double getSamp() {
        return samp;
    }

    public int getType() {
        return type;
    }

    public void addToPlot(Plot2DPanel plot) {
                addToPlot(plot, getNewColor(plot.plotCanvas.plots.size()));
    }

    public void addToPlot(Plot2DPanel plot, Color color) {
        double[] x;
        if (this.getArgs().length == 0) {
            x = Array.increment(
                    this.getValues().length, this.getBegin(), this.getSamp());
        } else {
            x = this.getArgs();
        }

        Plot a_plot = new LinePlot(this.getName(),
               // getNewColor(plot.plotCanvas.plots.size()),
                color,
                Array.mergeColumns(x, this.getValues()));
        plot.addPlot(a_plot);
        //plot.addLegend("---- "+this.getName());

        //plot.plotLegend.add(new JLabel("---- "+this.getName()));
        /*plot.addLabel("---- "+this.getName(), color, (args==null)?begin+samp*(values.length/2.0):(args[args.length-1]-args[0])/2.0,
                DataProcessor.getMean(this.getValues()));*/
    }

    protected Color getNewColor(int sz) {
        return COLORLIST[sz
                % COLORLIST.length];
    }

    public String getLabelX() {
        return labelX;
    }

    public void setLabelX(String labelX) {
        this.labelX = labelX;
    }

    public String getLabelY() {
        return labelY;
    }

    public void setLabelY(String labelY) {
        this.labelY = labelY;
    }



    @Override
    public String toString() {
        return name + " (" + names_type[type] + ")-" + values.length;
    }
}
